<template>
  <div style="background: #F0F8FF">
    <div style="width: 1200px; margin: 0 auto">
      <!-- 头部 -->
      <div style="display: flex; background-color: #fff; height: 60px; line-height: 60px; border-bottom: 1px solid #eee">
        <!-- 标题 -->
        <div style="width: 240px; display: flex; padding-left: 30px">
          <div style="width: 60px; display: flex; align-items: center;">
            <img src="../../assets/logo.png" style="width: 40px;">
          </div>
          <div style="flex: 1; font-size: 20px; color: #1E90FF; font-weight: bold">在线商城系统</div>
        </div>
        <!-- 菜单 -->
        <div style="flex: 1">
          <el-menu :default-active="$route.path" mode="horizontal" router>
            <el-menu-item index="/front/home">首页</el-menu-item>
            <el-menu-item index="/front/article">公告信息</el-menu-item>
            <el-menu-item index="/front/goodsList">商品列表</el-menu-item>
            <el-menu-item v-show="user.userName" index="/front/cart">购物车</el-menu-item>
            <el-menu-item v-show="user.userName" index="/front/orderList">我的订单</el-menu-item>
            <el-menu-item v-show="user.userName" index="/front/collect">我的收藏</el-menu-item>
          </el-menu>
        </div>
        <!-- 登录/注册 -->
        <div style="width: 160px">
          <div v-if="!user.userName" style="text-align: right; padding-right: 30px">
            <el-button @click="$router.push('/login')">登录</el-button>
            <el-button @click="$router.push('/register')">注册</el-button>
          </div>
          <div v-else>
            <el-dropdown style="width: 150px; cursor: pointer; text-align: right">
              <div style="display: inline-block">
                <img :src="user.avatarImg" alt="" 
                     style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
                <span>{{ user.nickname }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
              </div>
              <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
                <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                  <router-link to="/front/password">修改密码</router-link>
                </el-dropdown-item>
                <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                  <router-link to="/front/person">个人信息</router-link>
                </el-dropdown-item>
                <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                  <span style="text-decoration: none" @click="logout">退出</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>

      <!-- 主体内容 -->
      <div style="width: 100%; padding: 20px 0">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Front",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    // 监听用户刷新事件
    this.$on('refreshUser', this.refreshUser);
  },
  beforeDestroy() {
    // 组件销毁前移除监听
    this.$off('refreshUser', this.refreshUser);
  },
  methods: {
    logout() {
      this.$store.commit("logout");
      this.$message.success("退出成功");
    },

    // 刷新用户信息
    refreshUser() {
      this.user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {};
    }
  }
}
</script>

<style>
.item{
  display: inline-block;
  width: 100px;

  text-align: center;
  cursor: pointer
}
.item a {
  color: 	#1E90FF;
}
.item:hover{
  background-color: 	LightPink;
}
</style>
